package com.atguigu.gmall.item.service;

import com.agtuigu.gmall.pms.entity.*;
import com.agtuigu.gmall.pms.vo.ItemGroupVo;
import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.item.feign.GmallPmsClient;
import com.atguigu.gmall.item.feign.GmallSmsClient;
import com.atguigu.gmall.item.feign.GmallWmsClient;
import com.atguigu.gmall.item.vo.ItemVo;
import com.atguigu.gmall.sms.vo.SaleVo;
import com.atguigu.gmall.wms.entity.WareSkuEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemService {


    @Autowired
    private GmallPmsClient pmsClient;

    @Autowired
    private GmallSmsClient smsClient;

    @Autowired
    private GmallWmsClient wmsClient;


    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    public ItemVo queryItemVo(Long skuId) {
        ItemVo itemVo = new ItemVo();
        //1.设置skuId
        itemVo.setSkuId(skuId);
        //2.根据skuId查询商品信息
        CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync(() -> {
            Resp<SkuInfoEntity> skuInfoEntityResp = pmsClient.querySkuById(skuId);
            SkuInfoEntity skuInfoEntity = skuInfoEntityResp.getData();
            if (skuInfoEntity == null) {
                return itemVo;
            }
            //2.1设置参数
            itemVo.setSkuTitle(skuInfoEntity.getSkuTitle());
            itemVo.setSubTitle(skuInfoEntity.getSkuSubtitle());
            itemVo.setPrice(skuInfoEntity.getPrice());
            itemVo.setWeight(skuInfoEntity.getWeight());
            itemVo.setSpuId(skuInfoEntity.getSpuId());
            //获取spuId
            return skuInfoEntity;
        }, threadPoolExecutor);

        CompletableFuture<Void> spuCompletableFuture = completableFuture.thenAcceptAsync(sku -> {
            //3.根据SpuId查询spu信息
            Resp<SpuInfoEntity> spuInfoEntityResp = pmsClient.querySpuById(((SkuInfoEntity) sku).getSpuId());
            SpuInfoEntity spuInfoEntity = spuInfoEntityResp.getData();
            if (spuInfoEntity != null) {
                itemVo.setSpuName(spuInfoEntity.getSpuName());
            }
        }, threadPoolExecutor);


        //4.根据所属分类id查询所属分类数据
        //4.1拿到所属分类id
        CompletableFuture<Void> categoryCompletableFuture = completableFuture.thenAcceptAsync(sku -> {
            Resp<CategoryEntity> categoryEntityResp = pmsClient.queryCategoryById(((SkuInfoEntity) sku).getCatalogId());
            CategoryEntity categoryEntity = categoryEntityResp.getData();
            if (categoryEntity != null) {
                //4.2设置商品分类数据
                itemVo.setCategoryEntity(categoryEntity);
            }
        }, threadPoolExecutor);


        //5.拿到品牌id查询品牌信息
        CompletableFuture<Void> brandCompletableFuture = completableFuture.thenAcceptAsync(sku -> {
            Resp<BrandEntity> brandEntityResp = pmsClient.queryBrandById(((SkuInfoEntity) sku).getBrandId());
            BrandEntity brandEntity = brandEntityResp.getData();
            if (brandEntity != null) {
                //5.1设置品牌数据
                itemVo.setBrandEntity(brandEntity);
            }
        }, threadPoolExecutor);

        //6.根据skuId查询图片信息
        CompletableFuture<Void> imagesCompletableFuture = CompletableFuture.runAsync(() -> {
            Resp<List<SkuImagesEntity>> listResp = pmsClient.querySkuImagesBySkuId(skuId);
            List<SkuImagesEntity> skuImagesEntities = listResp.getData();
            itemVo.setPics(skuImagesEntities);
        }, threadPoolExecutor);


        //7.获取营销信息营销信息
        CompletableFuture<Void> saleCompletableFuture = CompletableFuture.runAsync(() -> {
            Resp<List<SaleVo>> SalesResp = smsClient.querySalesBySkuId(skuId);
            List<SaleVo> saleVoList = SalesResp.getData();
            itemVo.setSalse(saleVoList);
        }, threadPoolExecutor);


        //根据skuId查询库存信息
        CompletableFuture<Void> wareSkuCompletableFuture = CompletableFuture.runAsync(() -> {
            Resp<List<WareSkuEntity>> wareResp = wmsClient.queryWareSkusBySkuId(skuId);
            List<WareSkuEntity> wareSkuEntities = wareResp.getData();
            itemVo.setStore(wareSkuEntities.stream().allMatch(wareSkuEntity -> wareSkuEntity.getStock() > 0));
        }, threadPoolExecutor);

        //8.获取销售属性信息
        CompletableFuture<Void> skuSaleAttrValueCompletableFuture = CompletableFuture.runAsync(() -> {
            Resp<List<SkuSaleAttrValueEntity>> skuSaleAttrValueBySkuId = pmsClient.querySkuSaleAttrValueBySkuId(skuId);
            List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = skuSaleAttrValueBySkuId.getData();
            if (skuSaleAttrValueEntities != null) {
                itemVo.setSaleAttrs(skuSaleAttrValueEntities);
            }
        }, threadPoolExecutor);


        //9.根据spuId查询商品描述(海报)
        CompletableFuture<Void> spuInfoDescCompletableFuture = completableFuture.thenAcceptAsync(sku -> {
            Resp<SpuInfoDescEntity> spuInfoDescEntityResp = pmsClient.querySpuDescBySpuId(((SkuInfoEntity) sku).getSpuId());
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescEntityResp.getData();
            if (spuInfoDescEntity != null) {
                String decript = spuInfoDescEntity.getDecript();
                String[] split = StringUtils.split(decript, ",");
                itemVo.setImages(Arrays.asList(split));
            }
        }, threadPoolExecutor);

        //根据spuId和cateId查询组及组以下的规格参数(带值)
        CompletableFuture<Void> itemGroupCompletableFuture = completableFuture.thenAcceptAsync(sku -> {
            Resp<List<ItemGroupVo>> itemGroupResp = pmsClient.queryItemGroupVoByCidAndSpuId(((SkuInfoEntity) sku).getCatalogId(), ((SkuInfoEntity) sku).getSpuId());
            List<ItemGroupVo> itemGroupVos = itemGroupResp.getData();
            itemVo.setGroups(itemGroupVos);
        }, threadPoolExecutor);

        CompletableFuture.allOf(spuCompletableFuture, categoryCompletableFuture, brandCompletableFuture, imagesCompletableFuture, saleCompletableFuture,
                wareSkuCompletableFuture, skuSaleAttrValueCompletableFuture, spuInfoDescCompletableFuture, itemGroupCompletableFuture).join();

        return itemVo;
    }
}
